package com.xhsj.user.hr.system.web;

import com.xhsj.user.hr.system.vo.ConfirmSocialSecurityVo;
import com.xhsj.user.utils.StringUtils;
import com.xhsj.user.utils.UserUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


import com.xhsj.user.hr.system.service.TbHrUserSocialSecurityService;
import com.xhsj.user.hr.system.entity.TbHrUserSocialSecurity;

import javax.validation.Valid;


/**
 * <p>
 * 用户社保凭证表 前端控制器
 * </p>
 *
 * @author suxiaolin
 * @since Aug 5, 2021 6:20:16 AM
 */
@Slf4j
@Api(tags = "用户社保凭证表")
@RestController
@RequestMapping("/system/tbHrUserSocialSecurity")
public class TbHrUserSocialSecurityController {

	@Autowired
    public TbHrUserSocialSecurityService service;


	/**
	 * 保存/修改
	 * @author suxiaolin
	 * @date Aug 5, 2021 6:20:16 AM
	 */
	@ApiOperation(value = "用户社保凭证表,保存/修改", notes = "用户社保凭证表,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:hr:user:social:security:edit')")
	public Message save(@RequestBody TbHrUserSocialSecurity data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}
	}

	/**
	 * 获取用户是否上传社保凭证
	 * @author 苏小林
	 * @date 2021/8/6 9:46
	 * @param: userId
	 * @return: com.xhsj.user.utils.Message
	 */
	@GetMapping("/getUserSocialSecurity")
	public Message getUserSocialSecurity(@RequestParam(value = "userId",required = false) Long userId,
										 @RequestParam(value = "dataId",required = false) Long dataId) {
		try {
			if (StringUtils.isBlank(userId)) {
				userId = UserUtils.getLoginAppUser().getUserId();
			}
			TbHrUserSocialSecurity tbHrUserSocialSecurity = service.getUserSocialSecurity(userId,dataId);
			return Message.success("成功",tbHrUserSocialSecurity);
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}

	/**
	 * 确认 工龄
	 * @author 苏小林
	 * @date 2021/8/11 13:17
	 * @param: userId
	 * @param: dataId
	 * @return: com.xhsj.user.utils.Message
	 */
	@PostMapping("/handleConfirmSocialSecurity")
	public Message handleConfirmSocialSecurity(@RequestBody @Valid ConfirmSocialSecurityVo entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		try {

			service.handleConfirmSocialSecurity(entity);

			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,"失败");
		}
	}



	/**
	 * 通过id删除数据
	 * @author suxiaolin
	 * @date Aug 5, 2021 6:20:16 AM
	 */
	@ApiOperation(value = "用户社保凭证表删除", notes = "通过id删除用户社保凭证表信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    // @PreAuthorize("hasAuthority('tb:hr:user:social:security:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}
	}


    /**
    * 分页查询数据
    * @author suxiaolin
    * @date Aug 5, 2021 6:20:16 AM
    */
	@ApiOperation(value = "分页查询用户社保凭证表list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:hr:user:social:security:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrUserSocialSecurity> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author suxiaolin
    * @date Aug 5, 2021 6:20:16 AM
    */
	@ApiOperation(value = "查询用户社保凭证表所有数据list", notes = "查询用户社保凭证表所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    // @PreAuthorize("hasAuthority('tb:hr:user:social:security:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author suxiaolin
    * @date Aug 5, 2021 6:20:16 AM
    */
	@ApiOperation(value = "通过id获取用户社保凭证表实体类", notes = "通过id获取用户社保凭证表实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

}

